package com.ming.sportdemo.Aop;

import com.ming.sportdemo.Exception.RoleException;
import com.ming.sportdemo.bean.LoginLog;
import com.ming.sportdemo.bean.User;
import com.ming.sportdemo.service.LoginLogService;
import com.ming.sportdemo.service.UserService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Aspect
@Repository
public class
UserLogAop {

    @Autowired
    LoginLogService loginLogService;

    @Autowired
    UserService userService;

    LoginLog loginLog = new LoginLog();

    @Pointcut("@annotation(com.ming.sportdemo.annotation.Log)")
    public void log(){

    }

    @Around("log()")
    public Object round(ProceedingJoinPoint joinPoint) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Date date = new Date();
        loginLog.setUsername(getUserNameFromAop(joinPoint));
        loginLog.setLogInfo("测试登录");
        loginLog.setCurDate(date);
        loginLog.setLoginIp(request.getRemoteAddr());
        loginLog.setLoginURL(request.getRequestURI());
        Object[] args = joinPoint.getArgs();
        Object proceed = null;
        User user = (User) args[0];
        System.out.println(user);
        User byId = userService.getUserByMessage(user.getUsername(),user.getPassword());
        try {
            if(byId!=null){
                loginLog.setReturnMsg("成功登录");
            }else {
                loginLog.setReturnMsg("登陆失败");
            }
            proceed = joinPoint.proceed();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            loginLogService.save(loginLog);
        }
        return proceed;
    }


/*
    @Before("log()")
    public void before(JoinPoint joinPoint){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Date date = new Date();
        loginLog.setUsername(getUserNameFromAop(joinPoint));
        loginLog.setLogInfo("测试登录");
        loginLog.setCurDate(date);
        loginLog.setLoginIp(request.getRemoteAddr());
        loginLog.setLoginURL(request.getRequestURI());
    }
    @AfterReturning(returning = "ret",pointcut = "log()")
    public void afterReturning(Object ret){
        loginLog.setReturnMsg("成功登录");
    }
    @AfterThrowing("log()")
    public void afterThrowing() {
        loginLog.setReturnMsg("登陆失败");
    }

    @After("log()")
    public void after(){
        loginLogService.save(loginLog);
        LoginLog loginLog = new LoginLog();
        this.loginLog = loginLog;
    }
*/

    private String getUserNameFromAop(JoinPoint joinPoint){
        Object[] objects = joinPoint.getArgs();
        User user = (User) objects[0];
        return user.getUsername();
    }
}
